﻿Imports System.Data
Imports System.Data.OleDb
Imports System.IO
Imports System.IO.StreamWriter

Public Class Bienvenida
    Public log As StreamWriter = My.Computer.FileSystem.OpenTextFileWriter("log.txt", True)
    Public usuario As String

    Private Sub ButtonEntrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b_Entrar.Click
        Dim conexion As New OleDb.OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0; Data Source=nuevozoo.accdb")
        'Vuelco el codigo de usuario en una variable.Si al hacer el casting a string no da error
        'significa que es valido, sino se activara el error provider
        ErrorProvider.SetError(lab_Error, "")
        lab_Error.Text = ""

        Try
            Dim contra As New OleDbCommand("SELECT cod_empleado from empleados where contraseña='" & text_Contraseña.Text & "' and nombre='" & text_Nombre.Text & "'", conexion)
            conexion.Open()
            Dim a As Object = contra.ExecuteScalar
            Dim cod As String = a.ToString
            conexion.Close()
            usuario = text_Nombre.Text
            text_Contraseña.Text = ""
            text_Nombre.Text = ""
            GestionGeneral.Show()
            Me.Hide()
        Catch ex As Exception
            ErrorProvider.SetError(lab_Error, "Error")
            lab_Error.Text = "Error: datos incorrectos"
            log.WriteLine("Fecha: " & Date.Now & "Pantalla: " & Me.Name & " Error: " & lab_Error.Text)
            text_Contraseña.Text = ""
            text_Nombre.Text = ""
            text_Nombre.Focus()
            log.Flush()
        End Try

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        GestionGeneral.Show()
        Me.Hide()
    End Sub

    Private Sub text_Contraseña_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles text_Contraseña.KeyDown
        If e.KeyCode = Keys.Enter Then
            b_Entrar.PerformClick()
        End If

    End Sub

End Class
